import mockjs from "mockjs";

function processRenponse(f) {
    // option 来源于 f
    function process(option) {
        const body = JSON.parse(option.body) || {};
        const query = {};
        let index = option.url.indexOf("?");
        if (index > 0) {
            // http://www.vaidu.com/index/getData?id=123&user=456
            option.url
                .slice(index + 1)
                .split("&")
                .forEach((item) => {
                    let [key, value] = item.split("=");
                    query[key] = value;
                });
        }
        return f({
            url: option.url,
            body,
            query,
        });
    }
    return process;
}

const files = import.meta.glob("./modules/*.js");
for (const path in files) {
    // console.log(api);
    // console.log(files[api]);
    files[path]().then((mod) => {
        // console.log(mod.default);
        mod.default.forEach((api) => {
            // console.log(api);
            mockjs.mock(api.url, api.method ? api.method : /get/, processRenponse(api.response));
        });
    });
}